package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.android.volley.Response;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.Constants;
import com.fqdf.framework.android.net.response.NetBaseWrapper;
import com.fqdf.framework.android.util.DialogUtil;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

public class QRCaptureActivity extends BaseRequestActivity{

    private CaptureFragment captureFragment;

    private CheckBox light;

    private TextView lightContent;

    private int carId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcapture);
        captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.qrcapture_scan);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.qr_container, captureFragment).commit();

        carId = getIntent().getIntExtra(Constants.BUNDLE_DATA,0);
        initCurView();
    }

    private void initCurView() {
        light = (CheckBox) findViewById(R.id.qr_checkbox_light);
        lightContent = (TextView) findViewById(R.id.qr_light_hint);
        ImageButton back = (ImageButton) findViewById(R.id.back);


        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        light.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CodeUtils.isLightEnable(isChecked);
                if (isChecked) {
                    lightContent.setText(R.string.str_qr_light_on);
                } else {
                    lightContent.setText(R.string.str_qr_light_off);
                }
            }
        });

    }


    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {

            /*
            * 扫码成功之后判断订单状态
            * */
            confirmCharge(result);
        }

        @Override
        public void onAnalyzeFailed() {
            DialogUtil.ToastMessage(QRCaptureActivity.this, getString(R.string.str_qr_error));
        }
    };

    private void confirmCharge(String chargeResult){
        RequestOptService.confirmCharge(getApplicationContext(), carId, chargeResult, new Response.Listener<NetBaseWrapper>() {
            @Override
            public void onResponse(NetBaseWrapper response) {
                DialogUtil.ToastMessage(QRCaptureActivity.this,"操作成功！");
                QRCaptureActivity.this.finish();
            }
        },getDefaultErrorListener());
    }



}
